<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tpshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 个人学习免费, 如果商业用途务必到TPshop官网购买授权.
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 *
 */ 
namespace app\pc\controller;
use app\common\logic\UsersLogic;
use app\common\logic\CartLogic;
use app\common\logic\OrderLogic;
use think\Db;
use think\Verify;

class Member extends Base {

    public function index(){

        return $this->fetch('member/index');
    }

    public function login(){

        if (IS_POST){
            p($_POST);
            $username = trim(I('post.username'));
            $password = trim(I('post.password'));
            $verify_code = I('post.verify_code');

            $verify = new Verify();
            if (!$verify->check($verify_code,'user_login'))
            {
                $res = array('status'=>0,'msg'=>'验证码错误');
                exit(json_encode($res));
            }

            $logic = new UsersLogic();
            $res = $logic->login($username,$password);

            if($res['status'] == 1){
                $res['url'] =  htmlspecialchars_decode(I('post.referurl'));
                session('user',$res['result']);
                setcookie('user_id',$res['result']['user_id'],null,'/');
                setcookie('is_distribut',$res['result']['is_distribut'],null,'/');
                $nickname = empty($res['result']['nickname']) ? $username : $res['result']['nickname'];
                setcookie('uname',urlencode($nickname),null,'/');
                setcookie('cn',0,time()-3600,'/');
                $cartLogic = new CartLogic();
                $cartLogic->setUserId($res['result']['user_id']);
                $cartLogic->doUserLoginHandle();// 用户登录后 需要对购物车 一些操作
                $orderLogic = new OrderLogic();
                $orderLogic->setUserId($res['result']['user_id']); //登录后将超时未支付订单给取消掉
                $orderLogic->abolishOrder();
            }
            exit(json_encode($res));
        }
    }

    /**
     * 注册
     * /mobile/User/reg.html
     * */
    public function register(){
        $reg_sms_enable = tpCache('sms.regis_sms_enable');
        $reg_smtp_enable = tpCache('smtp.regis_smtp_enable');
        if(IS_POST){
            $logic = new UsersLogic();
            //验证码检验
//            $this->verifyHandle('user_reg');
            $username = I('post.username','');
            $password = I('post.password','');
            $password2 = I('post.password2','');
            $code = I('post.code','');
            $scene = I('post.scene', 1);
            $session_id = session_id();
            if(check_mobile($username)){
                if($reg_sms_enable){   //是否开启注册验证码机制
                    //手机功能没关闭
                    $check_code = $logic->check_validate_code($code, $username, 'phone', $session_id, $scene);
                    if($check_code['status'] != 1){
                        $this->ajaxReturn($check_code);
                    }
                }else{
                    if(!$this->verifyHandle('user_reg')){
                        $this->ajaxReturn(['status'=>-1,'msg'=>'图像验证码错误']);
                    };
                }
            }
            if(check_email($username)){
                if($reg_smtp_enable){        //是否开启注册邮箱验证码机制
                    //邮件功能未关闭
                    $check_code = $logic->check_validate_code($code, $username);
                    if($check_code['status'] != 1){
                        $this->ajaxReturn($check_code);
                    }
                }else{
                    if(!$this->verifyHandle('user_reg')){
                        $this->ajaxReturn(['status'=>-1,'msg'=>'图像验证码错误']);
                    };
                }
            }
            $invite = I('invite');
            if(!empty($invite)){
                $invite = get_user_info($invite,2);//根据手机号查找邀请人
            }
            $data = $logic->reg($username,$password,$password2,0,$invite);
            if($data['status'] != 1){
                $this->ajaxReturn($data);
            }
            session('user',$data['result']);
            setcookie('user_id',$data['result']['user_id'],null,'/');
            setcookie('is_distribut',$data['result']['is_distribut'],null,'/');
            $nickname = empty($data['result']['nickname']) ? $username : $data['result']['nickname'];
            setcookie('uname',$nickname,null,'/');
            $cartLogic = new CartLogic();
            $cartLogic->setUserId($data['result']['user_id']);
            $cartLogic->doUserLoginHandle();// 用户登录后 需要对购物车 一些操作
            $this->ajaxReturn($data);
            exit;
        }
        $this->assign('regis_sms_enable',tpCache('sms.regis_sms_enable')); // 注册启用短信：
        $this->assign('regis_smtp_enable',tpCache('smtp.regis_smtp_enable')); // 注册启用邮箱：
        $sms_time_out = tpCache('sms.sms_time_out')>0 ? tpCache('sms.sms_time_out') : 120;
        $this->assign('sms_time_out', $sms_time_out); // 手机短信超时时间
        return $this->fetch();

    }

    /**
     * 退出
     * */
    public function logout(){

        setcookie('uname','',time()-3600,'/');
        setcookie('cn','',time()-3600,'/');
        setcookie('user_id','',time()-3600,'/');
        setcookie('PHPSESSID','',time()-3600,'/');
        session_unset();
        session_destroy();
        //$this->success("退出成功",U('Home/Index/index'));
        $this->redirect('pc/Index/index');
        exit;
    }

    /**
     * 登录验证
     * */
    public function checkLogin(){

        if( ! $this->user_id) {

            $this->redirect(U('Index/index'));
        }

    }

    public function verify()
    {

        $config = array(
            'fontSize' => 30,
            'length' => 4,
            'useCurve' => false,
            'useNoise' => false,
            'reset' => false
        );
        $Verify = new Verify($config);
        $Verify->entry("user_login");
        exit();
    }


}